package com.xxh.kfc.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisUtils {
    public  static SqlSessionFactory sqlSessionFactory;
    //读取mysql数据库配置
    static {
        //通过配置文件获取数据库连接信息
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //拿到Sql会话
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
